from django.shortcuts import render

# Create your views here.

from booktest.models import BookInfo,HeroInfo
from datetime import date
from django.db.models import F,Q,Sum,Avg

book = BookInfo()
book.btitle = '西游记'
book.bpub_date = '1991-1-1'
book.save()

hero1 = HeroInfo(
    hname = '孙悟空',
    hgender = 1,
    hcomment= '72变化',
    hbook=book
)
hero1.save()

book2 = BookInfo.objects.create(
    btitle = '梦三国',
    bpub_date = '2011-11-11',
    bread = 10,
    bcomment = 10
)

BookInfo.objects.get(id = 1)

BookInfo.objects.all()

BookInfo.objects.count()

# 演示过滤查询
BookInfo.objects.filter(id__exact = 1)
BookInfo.objects.filter(id = 1)

BookInfo.objects.filter(btitle__contains='湖')

BookInfo.objects.filter(btitle__endswith = '部')

BookInfo.objects.filter(btitle__isnull=False)

BookInfo.objects.filter(id__in=[2,4])

BookInfo.objects.filter(id__gt=2)

BookInfo.objects.exclude(id = 3)

BookInfo.objects.filter(bpub_date__year='1980')

BookInfo.objects.filter(bpub_date__gt= '1990-1-1')

BookInfo.objects.filter(bread__gt=F('bcomment'))

BookInfo.objects.filter(Q(bread__gt=20) & Q(pk__lt=3))
BookInfo.objects.filter(bread__gt=20,pk__lt =3)

BookInfo.objects.filter(Q(bread__gt=20) | Q(pk__lt=3))

BookInfo.objects.filter(~Q(id = 3))

BookInfo.objects.aggregate(Sum('bread'))

BookInfo.objects.all().order_by('bread')
BookInfo.objects.all().order_by('-bread')

book3 = BookInfo.objects.get(id=1)
book3.heroinfo_set.all()

hero2 = HeroInfo.objects.get(hname='郭靖')
hero2.hbook

BookInfo.objects.filter(heroinfo__hname='郭靖')
BookInfo.objects.filter(heroinfo__hcomment__contains='龙')


HeroInfo.objects.filter(hbook__btitle = '天龙八部')
HeroInfo.objects.filter(hbook__btitle__contain = '湖')

book = BookInfo.objects.get(btitle='西游记')
book.btitle = '西游记后传'
book.save()

BookInfo.objects.filter(btitle = '梦三国').update(btitle = '第二版')
